<?php
/**
 * Functions
 * 
 * General functions used in application
 * 
 * @author Gabriel PREDA
 * @copyright  Copyright (c) 2008-2009 Acceleron Team
 * @version    $Id: Functions.php 38 2008-12-03 20:11:00Z gabriel.preda $
 */

/**
 * Autoloader for classes
 *
 * @param string $ClassName
 */
function __autoload($ClassName) {
	
	$PossibleLocations = array(
		'lib',
		'sys',
		'business'
		);
	
	foreach ($PossibleLocations as $pl)
	{
		$fn = $pl . DIRECTORY_SEPARATOR . $ClassName . '.php';
		if (file_exists($fn) && is_readable($fn) && (filesize($fn) != 0))
		{
			require_once $fn;
		}
		
	}
}

/**
 * This is the application Error Handler that overwrites the Error Handler of PHP
 *
 * @param int $ErrNo
 * @param string $ErrStr
 * @param string $ErrFile
 * @param int $ErrLine
 * @param mixed $ErrContext
 * @return true
 */
function AcceleronErrorHandler($ErrNo, $ErrStr, $ErrFile, $ErrLine, $ErrContext)
{
	$ErrorMoment		= date('c');
	$FullErrorReport	= " $ErrorMoment ::: $ErrFile ::: $ErrLine ::: $ErrStr ";

	if ($ErrNo >= E_USER_WARNING)
	{
		$ErrContextFormated	= print_r($ErrContext, true);

		$FullErrorReport .= "
------------------------------------------------------------------------
$ErrContextFormated
------------------------------------------------------------------------\n\n";
	}

	if ($ErrNo > 2047)
	{
		// error_log('ERROR ::: ' . $FullErrorReport, 1, 'gabriel.preda@abroadsoftware.com');
		return true;
	}

	switch ($ErrNo)
	{
		case E_USER_ERROR:
		case E_USER_WARNING:
			error_log('ERROR ::: ' . $FullErrorReport, 1, 'gabriel.preda@abroadsoftware.com');
			error_log('ERROR ::: ' . $FullErrorReport, 0);
		break;

		case E_USER_NOTICE:
			error_log('NOTICE ::: ' . $FullErrorReport, 0);
		break;

		default:
			error_log($ErrNo . ' ::: ' . $FullErrorReport, 0);
		break;
	}

	/* Don't execute PHP internal error handler */
	return true;
}

set_error_handler('AcceleronErrorHandler');
